﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.ServiceProcess;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
using log4net.Config;

namespace FaxServerNET
{
    partial class FaxServerService : ServiceBase
    {
        private FaxCore fax_core;
        public FaxServerService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            XmlConfigurator.Configure();
            var basePath = Path.GetDirectoryName(this.GetType().Assembly.Location);
            var _log = log4net.LogManager.GetLogger(this.GetType());

            _log.DebugFormat("Using working base path: {0}",basePath);
            IApplicationContext context = new XmlApplicationContext(Path.Combine(basePath,"configuration","applicationContext.XML"));

            fax_core = (FaxCore)context.GetObject("fax_core");


            fax_core.RunServer();
        }

        protected override void OnStop()
        {
            if (fax_core != null)
            {
                fax_core.StopServer();
            }
        }
    }

    [RunInstaller(true)]
    public class FaxServerServiceInstaller : Installer
    {
        ServiceProcessInstaller processInstaller;
        ServiceInstaller serviceInstaller;

        public FaxServerServiceInstaller()
        {
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();

            processInstaller.Account = ServiceAccount.LocalSystem;
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = "FaxServerService";
            serviceInstaller.DisplayName = "FaxServer NET";
            serviceInstaller.Description = "FaxServer NET Service";

            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }
    }
}
